<?php
class SprintPlansController extends AppController {
	var $name = 'SprintPlans';
	var $uses = array('Sprint');
	var $paginate = array(
        'order' => array(
            'Sprint.name' => 'asc'
        )
    );
    
	function beforeFilter() {
		//		parent::beforeFilter();
		$this->layout = 'controlpanel';
	}
	
	function index() {
		$this->Sprint->recursive = 0;
		$sprints = $this->paginate('Sprint');
		$this->set('sprints',$sprints);
	}

	function add() {
		
		if (!empty($this->data)) {
			$this->Sprint->create();
			$this->data['Sprint']['projects_id'] = $this->Session->read('selectedProject');
			$this->data['Sprint']['startDate'] = $this->convertStrToDate($this->data['startDate']);
			$this->data['Sprint']['endDate'] = $this->convertStrToDate($this->data['endDate']);
			
			//debug($this->data);
			if ($this->Sprint->save($this->data)) {
				$this->Session->setFlash(__('Sprint is created', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('There is an error in creating Sprint. Please contact with administrator', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid sponsor', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$sponsor = $this->Sponsor->read(null, $id);
			if ($file = $this->Uploader->upload('sponsorLogo')) {
				$this->data['Sponsor']['sponsorLogo'] = $file['name'];
			} else {
				$this->data['Sponsor']['sponsorLogo'] = $sponsor['Sponsor']['sponsorLogo'];
			}
			if ($this->Sponsor->saveAll($this->data)) {
				$this->Session->setFlash(__('The sponsor has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sponsor could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Sponsor->read(null, $id);
		}
		
		$this->loadModel('State');
		$states = $this->State->find("list");
		
		$this->set(compact('states'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Sprint', true));
			$this->redirect(array('action'=>'index'));
		} else {
			if ($this->Sprint->delete($id)) {
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Sprint was not deleted', true));
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
	function search () {
		$this->paginate = array(
        	'conditions' => array('Sponsor.sponsorName LIKE' => '%'.$this->data['Sponsor']['sponsorKeyword'].'%')
   		);
		
		$this->set('sponsors', $this->paginate());
		$this->render('index');
	}
	
	private function convertStrToDate($date) {
		list($d,$m,$y) = explode('/', $date);

		$cDate = array(
			'month' => $m,
			'day' => $d,
			'year' => $y,
			'hour' => '00',
			'min' =>'00',
			'second' => '00'
			);
		return $cDate;
	}
	
//	function listSchools($sponsorId = null){
//		if ($sponsorId != null) {
//			$this->loadModel('School');
//			
//		}
//	}
}
?>